package fy.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import fy.util.DBUtil;

public class WsmbBean {
	private int id;
	private String title;
	private String filepath;
	private Date time;
	private int puton;
	private String filename;

	PreparedStatement statement=null;
	ResultSet rs = null;
	Connection connection = null;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getFilepath() {
		return filepath;
	}
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}
	public int getPuton() {
		return puton;
	}
	public void setPuton(int puton) {
		this.puton = puton;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}

	//增加页面内容
	public boolean doAdd(String title,String filepath,String time,int puton,String filename) {
		boolean add = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		//String sql = "select password from admin where username = " + username + "'";
		String sql = "insert into wsmb(title,filepath,time,puton,filename) values(?,?,?,?,?)";

		try {		
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, title);
			statement.setString(2, filepath);
			statement.setString(3, time);
			statement.setInt(4, puton);
			statement.setString(5, filename);


			int i = statement.executeUpdate();
			if (i>0) {
				add = true;
				System.out.println(add);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return add;

	}

	public int findCount() {
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from wsmb where puton=1";
		try {
			statement = connection.prepareStatement(sql);
			rs = statement.executeQuery();
			if (rs.next()) {
				count = rs.getInt("totalCount");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	public List<WsmbBean> findAllByLbForFY(Page page) {
		List<WsmbBean> list = new ArrayList<WsmbBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from wsmb where puton=1 order by time desc limit ?,? ";
		/* String sql = "select * from ssfwzx t order by time desc; */

		try {
			statement = connection.prepareStatement(sql);
			statement.setInt(1, page.getBeginIndex());
			statement.setInt(2, page.getEveryPage());
			rs = statement.executeQuery();
			while (rs.next()) {
				WsmbBean wsmbBean = new WsmbBean();
				wsmbBean.setId(rs.getInt("id"));
				wsmbBean.setTitle(rs.getString("title"));
				wsmbBean.setTime(rs.getDate("time"));
				wsmbBean.setFilename(rs.getString("filename"));
				wsmbBean.setFilepath(rs.getString("filepath"));
				wsmbBean.setPuton(rs.getInt("puton"));
				list.add(wsmbBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	public int findbackCount() {
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		int count = 0;
		String sql = "select count(*) totalCount from wsmb ";
		try {
			statement = connection.prepareStatement(sql);

			rs = statement.executeQuery();
			if (rs.next()) {
				count=rs.getInt("totalCount");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return count;

	}

	public List<WsmbBean> findbackForFY(Page page) {
		List<WsmbBean> list = new ArrayList<WsmbBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql = "select * from wsmb order by time desc limit ?,? ";
		try {
			statement = connection.prepareStatement(sql);

			statement.setInt(1, page.getBeginIndex());
			statement.setInt(2, page.getEveryPage());
			rs = statement.executeQuery();
			while(rs.next()) {
				WsmbBean wsmbBean = new WsmbBean();
				wsmbBean.setId(rs.getInt("id"));
				wsmbBean.setTitle(rs.getString("title"));
				wsmbBean.setTime(rs.getDate("time"));
				wsmbBean.setFilename(rs.getString("filename"));
				wsmbBean.setFilepath(rs.getString("filepath"));
				wsmbBean.setPuton(rs.getInt("puton"));
				list.add(wsmbBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}

	public List<WsmbBean> proselect(String tilString,Page page) {
		List<WsmbBean> list =new ArrayList<WsmbBean>();
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		String sql ="select * from wsmb where title like ? order by time desc limit ?,?" ;
		connection = dbUtil.geConnection();
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1,"%" + tilString + "%");
			statement.setInt(2, page.getBeginIndex());
			statement.setInt(3, page.getEveryPage());
			rs = statement.executeQuery();
			while(rs.next()) {
				WsmbBean wsmbBean = new WsmbBean();
				wsmbBean.setId(rs.getInt("id"));
				wsmbBean.setTitle(rs.getString("title"));
				wsmbBean.setTime(rs.getDate("time"));
				wsmbBean.setFilename(rs.getString("filename"));
				wsmbBean.setFilepath(rs.getString("filepath"));
				wsmbBean.setPuton(rs.getInt("puton"));
				list.add(wsmbBean);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return list;

	}
	
	public boolean doUpdata(int puton, int id) {
		boolean updata = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		// String sql = "select password from admin where username = " + username + "'";
		String sql = "update wsmb set puton=? where id =?";

		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setInt(1, puton);
			statement.setInt(2, id);
			
			int i = statement.executeUpdate();
			if (i > 0) {
				updata = true;
			}
			System.out.println(updata);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return updata;

	}
	public boolean doDel(int id) {
		boolean del = false;
		DBUtil dbUtil = new DBUtil();
		connection = dbUtil.geConnection();
		// String sql = "select password from admin where username = " + username + "'";
		String sql = "delete from wsmb where id =?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);

			statement.setInt(1, id);
			int i = statement.executeUpdate();
			if (i > 0) {
				del = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			dbUtil.close();
		}
		return del;

	}


}
